package com.yztc.thinkpad.mandemo.cooladapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.thinkpad.mandemo.R;
import com.yztc.thinkpad.mandemo.coolbean.CoolDataBean;

import java.util.List;

/**
 * Created by Administrator on 2016/10/5.
 * 帅吧 listview适配器
 */
public class CoolAdapter extends BaseAdapter {
    private List<CoolDataBean> mList;
    private Context context;

    public CoolAdapter(Context context, List<CoolDataBean> mList) {
        this.mList = mList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.cool_list_item, null);
            holder = new ViewHolder();
            holder.iv_cool_image = (ImageView) convertView.findViewById(R.id.iv_cool_image);
            holder.tv_cool_title = (TextView) convertView.findViewById(R.id.tv_cool_title);
            holder.tv_cool_clickCount = (TextView) convertView.findViewById(R.id.tv_cool_clickCount);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(mList.get(position).getImage())
                .placeholder(R.mipmap.default_banner)
                .error(R.mipmap.default_faile_image)
                .into(holder.iv_cool_image);
        if (mList.get(position).getTitle() != null) {
            holder.tv_cool_title.setText(mList.get(position).getTitle());
        }
//        Log.e("tag","*************"+mList.get(position).getClickCount());
        if (mList.get(position).getClickCount() + "" != null) {
            holder.tv_cool_clickCount.setText(mList.get(position).getClickCount() + "");
        }
        return convertView;
    }

    static class ViewHolder {
        TextView tv_cool_title, tv_cool_clickCount;
        ImageView iv_cool_image;
    }

    public void rush(List<CoolDataBean> list, boolean isClear) {
        if (isClear) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }
}
